<template>
    <div>
        <navigation-bar>
            <div class="searchContent">
                <!-- <el-input v-model="unitName" placeholder="请输入网关名称" size="mini" style="width: 150px; margin-right: 10px"></el-input> -->
                <custom-button type="search" style="margin-right: 10px" @searchClick="searchClick"></custom-button>
                <custom-button type="reset" style="margin-right: 10px" @resetClick="resetClick"></custom-button>
                <custom-button type="add" style="margin-right: 10px" @addClick="addClick"></custom-button>
            </div>
        </navigation-bar>

        <div style="margin-top: 10px">
            <table-index
                :dataSource="dataSource"
                :tableColumns="tableColumns"
                :btnList="btnList"
                :pageSize="pageSize"
                :pageCount="pageCount"
                :pageNo="pageNo"
                @pageChange="pageChange"
                @editRow="editRow"
                @viewRow="viewRow"
                @deleteRow="deleteRow"
                selection
                @handleSelectionChange="handleSelectionChange"
                :height="secondTableHeight"
            ></table-index>
        </div>

        <div v-if="dialogShow">
            <custom-dialog :title="dialogTitle" size="large" @dialogClose="dialogClose">
                <addPrice :type="type" @getData="getData" :setData="setData"></addPrice>
            </custom-dialog>
        </div>
    </div>
</template>

<script>
import {gatewayManageForm} from '@/assets/form/managementCenter/systemManage/gatewayManage'
import customDialog from '@/components/customComponents/customDialog'
import customButton from '@/components/customComponents/customForm/customButton'
import formIndex from '@/components/customComponents/customForm/formIndex'
import tableIndex from '@/components/customComponents/customeTable/iviewTable/singleViewTable'
import navigationBar from '@/components/customComponents/managementCenter/navigationBar'
import {AjaxCommon, printAddress} from '@/libs/apiCommon'
import {CardId} from '@/libs/cardId'
import {PopupCommon} from '@/libs/popupCommon'
import {SessionCommon} from '@/libs/sessionCommon'
import {Utility} from '@/libs/utility'
import addPrice from './addPrice.vue'
export default {
    components: {
        navigationBar,
        customButton,
        tableIndex,
        customDialog,
        formIndex,
        addPrice
    },
    data() {
        return {
            setData: {},
            buildItem: {},
            secondTableHeight: document.body.clientHeight - 290,
            selectOrgList: [],
            unitName: null,
            status: '',
            statusList: Utility.getDictList('status'),
            // table 相关参数  begin
            dataSource: [],
            tableColumns: [
                {
                    key: 'text',
                    title: '方案名称',
                    tree: true
                },
                {
                    key: 'defaultprice',
                    title: '默认价格'
                },
                {
                    key: 'loss',
                    title: '线损率',
                    slot: 'loss'
                },

                {
                    key: 'eqtype',
                    title: '能耗类型'
                },
                {
                    key: 'usetype',
                    title: '分项类型'
                },
                // {
                //     key: 'installTime',
                //     title: '时间段'
                // },
                // {
                //     key: 'installTime',
                //     title: '价格'
                // },
                {
                    title: '操作',
                    slot: 'action',
                    width: 120,
                    align: 'center'
                }
            ],
            btnList: ['edit', 'delete'],
            pageSize: 20,
            pageCount: 100,
            pageNo: 1,
            // table 相关参数  end

            // 弹窗是否显示
            dialogShow: false,
            // 弹窗标题 (新增修改不同)
            dialogTitle: '',

            // form的配置信息
            formConfig: null,
            type: 'add',

            typeCodeList: []
        }
    },
    created() {
        Utility.getDictListByTypeCode('WGXH').then((res) => {
            if (res && res.length > 0) {
                const data = res.map((t) => {
                    return {
                        value: t.value,
                        label: t.desc
                    }
                })
                this.typeCodeList = data
                this.formConfig = gatewayManageForm(data, 'add')
            }
        })
    },
    methods: {
        getData(d) {
            let self = this
            if (d.sche_guid) {
                AjaxCommon('/api/Eq/EditPriceScheme', 'post', d).then((res) => {
                    if (res?.data?.Code == 200) {
                        PopupCommon.notify('success', '编辑成功')
                        self.initPage()
                        self.dialogClose()
                    } else {
                        const msg = res?.data?.Message
                        if (msg) {
                            PopupCommon.notify('error', msg)
                        }
                    }
                })
            } else {
                delete d.sche_guid

                // 根据类型调用不同接口
                let url = '/api/Eq/AddPriceScheme'
                AjaxCommon(url, 'post', d).then((res) => {
                    if (res?.data?.Code == 200) {
                        PopupCommon.notify('success', '新增成功')
                        self.initPage()
                        self.dialogClose()
                    } else {
                        const msg = res?.data?.Message
                        if (msg) {
                            PopupCommon.notify('error', msg)
                        }
                    }
                })
            }
        },
        getHeight() {
            this.secondTableHeight = document.body.clientHeight - 290
        },
        customClick(isTrue) {
            this.useOrg(isTrue)
        },
        handleSelectionChange(d) {
            this.selectOrgList = d
        },
        formInputChange(e) {
            if (e.filedName == 'idCard') {
                const data = CardId.getInfoByCardId(e.value)
                if (data) {
                    let newData = e.allData
                    newData['birthday'] = data.birthday
                    newData['sex'] = data.sex
                    this.$refs.form.changeFormValue(newData)
                }
            }
        },
        submit(d) {
            let self = this
            console.log(d, 'd...............')
            if (d.org_guid) {
                // AjaxCommon('/api/OrgSysManage/EditCompanyOrg', 'post', d).then((res) => {
                //     if (res?.data?.Code == 200) {
                //         PopupCommon.notify('success', '编辑成功')
                //         self.initPage()
                //         self.dialogClose()
                //     } else {
                //         const msg = res?.data?.Message
                //         if (msg) {
                //             PopupCommon.notify('error', msg)
                //         }
                //     }
                // })
            } else {
                delete d.org_guid
                // 根据类型调用不同接口
                let url = '/api/OrgSysManage/AddCompanyOrg'
                AjaxCommon(url, 'post', d).then((res) => {
                    if (res?.data?.Code == 200) {
                        PopupCommon.notify('success', '新增成功')
                        self.initPage()
                        self.dialogClose()
                    } else {
                        const msg = res?.data?.Message
                        if (msg) {
                            PopupCommon.notify('error', msg)
                        }
                    }
                })
            }
        },
        dialogClose() {
            this.dialogShow = false
        },
        searchClick() {
            this.pageNo = 1
            this.initPage()
        },
        resetClick() {
            this.unitName = null
            this.searchClick()
        },
        addClick() {
            let formConfigItem = this.formConfig
            formConfigItem.map((t) => {
                Object.keys(t).map((res) => {
                    if (res == 'fieldName') {
                        t.filedValue = ''
                    }
                })
            })
            this.formConfig = formConfigItem
            this.type = 'add'
            this.dialogTitle = '新增'
            this.dialogShow = true
        },
        // 分页点击事件
        pageChange(p) {
            this.pageNo = p
            this.initPage()
        },
        async editRow(d) {
            this.type = 'edit'
            const formValidate = {
                sche_guid: d.id,
                schemeName: d.text,
                defaultPrice: d.defaultprice,
                eqType: d.eqtype,
                usetype: d.usetype,
                loss: d.loss,
                timeInterval: ''
            }
            this.setData = formValidate
            this.dialogTitle = '编辑'
            this.dialogShow = true
        },
        viewRow(d) {
            this.type = 'look'
            let formConfigItem = this.formConfig
            formConfigItem.map((t) => {
                Object.keys(t).map((res) => {
                    if (res == 'fieldName') {
                        t.filedValue = d.data[t[res]]
                    }
                })
            })
            this.formConfig = formConfigItem
            this.dialogTitle = '查看'
            this.dialogShow = true
        },
        // 启用或禁用组织网关 isTrue  true禁用 false 不禁用
        useOrg(isTrue) {
            const selectOrgList = this.selectOrgList
            if (selectOrgList.length < 1) {
                PopupCommon.notify('info', '请选择至少一个网关')
                return
            }
            const id = selectOrgList.map((t) => t.id).toString()
            let self = this
            let apiUrl = ''
            let tipMsg = ''
            if (!isTrue) {
                apiUrl = `api/OrgSysManage/ChangeOrgsDisable`
                tipMsg = '启用'
            } else {
                apiUrl = 'api/OrgSysManage/ChangeOrgsEnable'
                tipMsg = '禁用'
            }
            AjaxCommon(apiUrl, 'post', {orgids: id}).then((res) => {
                console.log(res, 'res...............')
                if (res?.data?.Code == 200) {
                    PopupCommon.notify('success', tipMsg + '成功')
                    self.initPage()
                } else {
                    const msg = res?.data?.Message
                    if (msg) {
                        PopupCommon.notify('error', msg)
                    }
                }
            })
        },
        deleteRow(d) {
            let self = this
            AjaxCommon(`/api/Eq/DeletePriceScheme?sche_guid=${d.id}`, 'post', {}).then((res) => {
                if (res?.data?.Code == 200) {
                    PopupCommon.notify('success', '删除成功')
                    AjaxCommon(`/api/Eq/DeleteTimeInterval?sche_guid=${d.id}`, 'post', {})
                    self.initPage()
                } else {
                    const msg = res?.data?.Message
                    if (msg) {
                        PopupCommon.notify('error', msg)
                    }
                }
            })
        },
        // 初始化事件
        initPage() {
            let self = this
            AjaxCommon(`/api/Eq/GetPriceSchemesPage?PageSize=${this.pageSize}&PageIndex=${this.pageNo}`, 'get', null).then((res) => {
                if (res?.data?.Code == 200) {
                    const data = res?.data.Message
                    console.log(data?.rows, 'data?.rows...')
                    self.dataSource = data?.rows
                    self.pageCount = parseInt(data?.total)
                } else {
                    PopupCommon.notify('error', res?.data?.msg)
                }
            })
        }
    },
    mounted() {
        this.$nextTick(() => {
            this.getHeight()
            this.initPage()
        })

        /*窗口自适应，关键代码*/
        window.onresize = () => {
            this.getHeight()
        }
    }
}
</script>

<style></style>
